package com.css.fxfzypg.modules.group.repository.impl;

import com.css.fxfzypg.modules.group.entity.GroupUserEntity;
import com.css.fxfzypg.modules.group.repository.GroupUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author
 * @program
 * @description
 * @create 2022/8/1 11:00
 **/
@Repository
public class GroupUserRepositoryimpl implements GroupUserRepository {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void deleteGroupIdAndUserId(String id) {
        jdbcTemplate.update("delete from ypg_group_user where group_id = ? ",id);
    }

    @Override
    public void saveGroupUser(List<GroupUserEntity> list) {
        for(GroupUserEntity groupUserEntity:list){
            String sql="insert into ypg_group_user values(:id, :groupId, :userId, :createTime, :createUser, :role)";
            SqlParameterSource source = new BeanPropertySqlParameterSource(groupUserEntity);
            namedParameterJdbcTemplate.update(sql, source);
        }
    }

    @Override
    public List<GroupUserEntity> selectGroupMemberAndGroupExpert(String groupId) {
        String sql="select * from ypg_group_user where group_id = ?";
        List<GroupUserEntity> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<GroupUserEntity>(GroupUserEntity.class), groupId);
        return list;
    }

    @Override
    public List<GroupUserEntity> getGroupId(String userId) {
        String sql="select group_id groupId from ypg_group_user where user_id = ?";
        List<GroupUserEntity> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<GroupUserEntity>(GroupUserEntity.class), userId);
        return list;
    }
}
